package com.medical.standard.test;

import com.medicalstandard.entity.Inspection;
import com.medicalstandard.entity.Standard;
import com.medicalstandard.entity.Term;
import com.medicalstandard.service.DataInputService;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class DataInputDemo {
    private static DataInputService service = new DataInputService();
    private static Scanner scanner = new Scanner(System.in);
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    public static void main(String[] args) {
        System.out.println("=== 医疗器械标准数据输入系统 ===");

        while (true) {
            showMenu();
            int choice = getIntInput("请选择操作: ");

            switch (choice) {
                case 1:
                    addStandard();
                    break;
                case 2:
                    updateStandard();
                    break;
                case 3:
                    deleteStandard();
                    break;
                case 4:
                    viewAllStandards();
                    break;
                case 5:
                    addInspection();
                    break;
                case 6:
                    updateInspection();
                    break;
                case 7:
                    viewInspections();
                    break;
                case 8:
                    addTerm();
                    break;
                case 9:
                    updateTerm();
                    break;
                case 10:
                    viewTerms();
                    break;
                case 0:
                    System.out.println("感谢使用，再见！");
                    return;
                default:
                    System.out.println("无效选择，请重新输入！");
            }
        }
    }

    private static void showMenu() {
        System.out.println("\n========== 主菜单 ==========");
        System.out.println("1. 添加标准信息");
        System.out.println("2. 修改标准信息");
        System.out.println("3. 删除标准信息");
        System.out.println("4. 查看所有标准");
        System.out.println("5. 添加检验要求");
        System.out.println("6. 修改检验要求");
        System.out.println("7. 查看检验要求");
        System.out.println("8. 添加术语定义");
        System.out.println("9. 修改术语定义");
        System.out.println("10. 查看术语定义");
        System.out.println("0. 退出系统");
        System.out.println("============================");
    }

    private static void addStandard() {
        System.out.println("\n--- 添加标准信息 ---");

        System.out.print("标准编号: ");
        String code = scanner.nextLine();

        System.out.print("标准名称: ");
        String name = scanner.nextLine();

        System.out.print("英文名称: ");
        String englishName = scanner.nextLine();

        System.out.print("国际标准引用型式: ");
        String referenceType = scanner.nextLine();

        System.out.print("发布日期 (yyyy-MM-dd): ");
        String publishDateStr = scanner.nextLine();

        System.out.print("实施日期 (yyyy-MM-dd): ");
        String implDateStr = scanner.nextLine();

        try {
            Date publishDate = sdf.parse(publishDateStr);
            Date implDate = sdf.parse(implDateStr);

            Standard standard = new Standard(code, name, englishName, referenceType, publishDate, implDate);
            boolean result = service.addStandard(standard);

            if (result) {
                System.out.println("标准信息添加成功！");
            } else {
                System.out.println("标准信息添加失败！");
            }
        } catch (Exception e) {
            System.out.println("日期格式错误！");
        }
    }

    private static void updateStandard() {
        System.out.println("\n--- 修改标准信息 ---");
        viewAllStandards();

        int id = getIntInput("请输入要修改的标准ID: ");
        Standard standard = service.getStandardById(id);

        if (standard == null) {
            System.out.println("未找到该标准！");
            return;
        }

        System.out.println("当前标准信息: " + standard);
        System.out.print("新标准编号 (当前: " + standard.getStandardCode() + "): ");
        String code = scanner.nextLine();
        if (!code.isEmpty()) standard.setStandardCode(code);

        System.out.print("新标准名称 (当前: " + standard.getStandardName() + "): ");
        String name = scanner.nextLine();
        if (!name.isEmpty()) standard.setStandardName(name);

        boolean result = service.updateStandard(standard);
        if (result) {
            System.out.println("标准信息修改成功！");
        } else {
            System.out.println("标准信息修改失败！");
        }
    }

    private static void deleteStandard() {
        System.out.println("\n--- 删除标准信息 ---");
        viewAllStandards();

        int id = getIntInput("请输入要删除的标准ID: ");
        boolean result = service.deleteStandard(id);

        if (result) {
            System.out.println("标准信息删除成功！");
        } else {
            System.out.println("标准信息删除失败！");
        }
    }

    private static void viewAllStandards() {
        System.out.println("\n--- 所有标准信息 ---");
        List<Standard> standards = service.getAllStandards();

        if (standards.isEmpty()) {
            System.out.println("暂无标准信息！");
            return;
        }

        for (Standard standard : standards) {
            System.out.println("ID: " + standard.getId() + " - " + standard);
        }
    }

    private static void addInspection() {
        System.out.println("\n--- 添加检验要求 ---");
        viewAllStandards();

        int standardId = getIntInput("请输入标准ID: ");
        Standard standard = service.getStandardById(standardId);

        if (standard == null) {
            System.out.println("标准不存在！");
            return;
        }

        System.out.print("项目编号: ");
        String projectNum = scanner.nextLine();

        System.out.print("项目名称: ");
        String projectName = scanner.nextLine();

        System.out.print("项目要求: ");
        String requirement = scanner.nextLine();

        Inspection inspection = new Inspection(standardId, projectNum, projectName, requirement);
        boolean result = service.addInspection(inspection);

        if (result) {
            System.out.println("检验要求添加成功！");
        } else {
            System.out.println("检验要求添加失败！");
        }
    }

    private static void updateInspection() {
        System.out.println("\n--- 修改检验要求 ---");
        viewAllStandards();

        int standardId = getIntInput("请输入标准ID: ");
        List<Inspection> inspections = service.getInspectionsByStandardId(standardId);

        if (inspections.isEmpty()) {
            System.out.println("该标准暂无检验要求！");
            return;
        }

        for (Inspection inspection : inspections) {
            System.out.println("ID: " + inspection.getInspectionId() + " - " + inspection);
        }

        int inspectionId = getIntInput("请输入要修改的检验要求ID: ");
        Inspection inspection = service.getInspectionById(inspectionId);

        if (inspection == null) {
            System.out.println("未找到该检验要求！");
            return;
        }

        System.out.print("新项目要求 (当前: " + inspection.getProjectRequirement() + "): ");
        String requirement = scanner.nextLine();
        if (!requirement.isEmpty()) inspection.setProjectRequirement(requirement);

        boolean result = service.updateInspection(inspection);
        if (result) {
            System.out.println("检验要求修改成功！");
        } else {
            System.out.println("检验要求修改失败！");
        }
    }

    private static void viewInspections() {
        System.out.println("\n--- 查看检验要求 ---");
        viewAllStandards();

        int standardId = getIntInput("请输入标准ID: ");
        List<Inspection> inspections = service.getInspectionsByStandardId(standardId);

        if (inspections.isEmpty()) {
            System.out.println("该标准暂无检验要求！");
            return;
        }

        for (Inspection inspection : inspections) {
            System.out.println(inspection);
        }
    }

    private static void addTerm() {
        System.out.println("\n--- 添加术语定义 ---");
        viewAllStandards();

        int standardId = getIntInput("请输入标准ID: ");
        Standard standard = service.getStandardById(standardId);

        if (standard == null) {
            System.out.println("标准不存在！");
            return;
        }

        System.out.print("项目编号: ");
        String projectNum = scanner.nextLine();

        System.out.print("术语名称: ");
        String termName = scanner.nextLine();

        System.out.print("术语定义: ");
        String definition = scanner.nextLine();

        Term term = new Term(standardId, projectNum, termName, definition);
        boolean result = service.addTerm(term);

        if (result) {
            System.out.println("术语定义添加成功！");
        } else {
            System.out.println("术语定义添加失败！");
        }
    }

    private static void updateTerm() {
        System.out.println("\n--- 修改术语定义 ---");
        viewAllStandards();

        int standardId = getIntInput("请输入标准ID: ");
        List<Term> terms = service.getTermsByStandardId(standardId);

        if (terms.isEmpty()) {
            System.out.println("该标准暂无术语定义！");
            return;
        }

        for (Term term : terms) {
            System.out.println("ID: " + term.getTermId() + " - " + term);
        }

        int termId = getIntInput("请输入要修改的术语定义ID: ");
        Term term = service.getTermById(termId);

        if (term == null) {
            System.out.println("未找到该术语定义！");
            return;
        }

        System.out.print("新术语定义 (当前: " + term.getDefinition() + "): ");
        String definition = scanner.nextLine();
        if (!definition.isEmpty()) term.setDefinition(definition);

        boolean result = service.updateTerm(term);
        if (result) {
            System.out.println("术语定义修改成功！");
        } else {
            System.out.println("术语定义修改失败！");
        }
    }

    private static void viewTerms() {
        System.out.println("\n--- 查看术语定义 ---");
        viewAllStandards();

        int standardId = getIntInput("请输入标准ID: ");
        List<Term> terms = service.getTermsByStandardId(standardId);

        if (terms.isEmpty()) {
            System.out.println("该标准暂无术语定义！");
            return;
        }

        for (Term term : terms) {
            System.out.println(term);
        }
    }

    private static int getIntInput(String prompt) {
        while (true) {
            try {
                System.out.print(prompt);
                return Integer.parseInt(scanner.nextLine());
            } catch (NumberFormatException e) {
                System.out.println("请输入有效的数字！");
            }
        }
    }
}